//=======================================================
// 作者：KingSley
// 描述：视图，解释模型，模型更新请求，发送用户输入给控制器，允许控制器选择视图
//=======================================================
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace MVC
{
    public class PlayerMsgView : MonoBehaviour
    {
        //UI
        private Text playerLevel;
        private Text playerExperience;
        private Text goldNum;
        private Button experienceUpButton;
        private Slider playerExperienceTextSlider;

        void Start()
        {
            // 初始化视图UI
            playerLevel = GameObject.Find("playerLevelText").GetComponent<Text>();
            playerExperience = GameObject.Find("playerExperienceText").GetComponent<Text>();
            goldNum = GameObject.Find("goldNumText").GetComponent<Text>();
            experienceUpButton = GameObject.Find("experienceUpButton").GetComponent<Button>();
            playerExperienceTextSlider = GameObject.Find("playerExperienceSlider").GetComponent<Slider>();

            // 委托事件绑定
            PlayerMsgModel.GetMod().OnLevelChange += SetLevel;
            PlayerMsgModel.GetMod().OnExperienceChange += SetExperience;
            PlayerMsgModel.GetMod().OnFullExperienceChange += SetFullExperience;
            PlayerMsgModel.GetMod().OnGoldNumChange += SetGoldNum;

            // View绑定按钮控制功能
            experienceUpButton.onClick.RemoveAllListeners();
            experienceUpButton.onClick.AddListener(
                PlayerMsgController.controller.OnExperienceUpButtonClick);
        }

        // 修改UILevel值
        public void SetLevel(int level)
        {
            playerLevel.text = level.ToString();
        }

        // 修改UI经验值
        public void SetExperience(int experience)
        {
            // 将字符串以“/”拆开
            string[] str = playerExperience.text.Split(new char[] { '/' });
            // 用新的经验值重组，并修改经验条情况
            playerExperience.text = experience + "/" + str[1];
            playerExperienceTextSlider.value = experience / float.Parse(str[1]);
        }
        
        public void SetFullExperience(int fullExiperience)
        {
            string[] str = playerExperience.text.Split(new char[] { '/' });

            playerExperience.text = str[0] + "/" + fullExiperience;
        }

        // 修改UI金币值
        public void SetGoldNum(int goldn)
        {
            goldNum.text = goldn.ToString();
        }
    }
}
